package com.jader.fragment;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jader.activities.GuJiaDetail;
import com.jader.adapter.MyAdapter_gujia;
import com.jader.beans.LunTan_gujia_Bens;
import com.jader.myviews.CustomListView;
import com.jader.myviews.CustomListView.OnLoadMoreListener;
import com.jader.myviews.CustomListView.OnRefreshListener;
import com.jader.util.JaderApplication;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zlp.jader.R;

public class LunTan_AuthenticateFragment extends Fragment {

	View view;
	List<LunTan_gujia_Bens> list = new ArrayList<LunTan_gujia_Bens>();
	LayoutInflater mInflater;
	MyAdapter_gujia adapter_gujia;
	EditText pingluneEditText;
	Button fasongbButton;
	// ListView listView;
	CustomListView listView;
	String url = JaderApplication.getIP()
			+ "/Jader/SelectAll?type=31&page_gujia=";
	int page_gujia = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = (View) inflater.inflate(R.layout.luntan_authenticate_fragment,
				null);

		initData();

		liebiao();
		getData();
		// showList(list);
		http(0);
		return view;
	}

	private void initData() {
		// TODO Auto-generated method stub

	}

	private void liebiao() {
		// 初始化尾部视图
		mInflater = LayoutInflater.from(view.getContext());
		listView = (CustomListView) view
				.findViewById(R.id.luntan_authenticate_listView);

		// 创建自定义适配器实例
		adapter_gujia = new MyAdapter_gujia(list, view.getContext());
		listView.setAdapter(adapter_gujia);
		http(0);
		listView.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				page_gujia = 0;
				http(0);
				// TODO 下拉刷新
				// loadData(0);
			}
		});

		listView.setOnLoadListener(new OnLoadMoreListener() {

			@Override
			public void onLoadMore() {
				// TODO 加载更多
				page_gujia = list.size();
				http(1);
				// loadData(1);

			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(getActivity(), GuJiaDetail.class);
				intent.putExtra("position", list.get(position - 1));
				startActivity(intent);
			}
		});
	}

	// 监听列表单击事件
	//

	private void getData() {

	}

	private void http(final int type) {
		HttpUtils httpUtils = new HttpUtils();
		httpUtils.send(HttpMethod.GET, url + page_gujia,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub
						Gson gson = new Gson();
						Type typelist = new TypeToken<List<LunTan_gujia_Bens>>() {
						}.getType();
						List<LunTan_gujia_Bens> liebiaos = gson.fromJson(
								arg0.result, typelist);
						if (type == 0) {
							// list.addAll(0,liebiaos);
							// 解决重复数据
							int result = 0;
							for (LunTan_gujia_Bens lunTan_gujia_Bens : liebiaos) {
								boolean flag = true;
								for (LunTan_gujia_Bens lunTan_gujia_Bens2 : list) {
									if (lunTan_gujia_Bens
											.equals(lunTan_gujia_Bens2)) {
										flag = false;
									}
								}
								if (flag) {
									list.add(result, lunTan_gujia_Bens);
									result++;
								}
							}
						} else if (type == 1) {
							// list.addAll(liebiaos);
							for (LunTan_gujia_Bens lunTan_gujia_Bens : liebiaos) {
								boolean flag = true;
								for (LunTan_gujia_Bens lunTan_gujia_Bens2 : list) {
									if (lunTan_gujia_Bens
											.equals(lunTan_gujia_Bens2)) {
										flag = false;
									}
								}
								if (flag) {
									list.add(lunTan_gujia_Bens);
								}
							}
						}
						adapter_gujia.notifyDataSetChanged();
						listView.onRefreshComplete();
						listView.onLoadMoreComplete();

					}
				});
	}
}